package com.voxel.sense.infomsg.module.task.check;

import com.example.com.common.app.BasePresenter;
import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.FileUtils;
import com.example.com.common.utils.ToastUtil;
import com.lzy.okgo.model.Progress;
import com.lzy.okserver.download.DownloadListener;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.net.helper.TaskManagerNetHelper;
import com.voxel.sense.infomsg.utils.AppFileUtil;
import com.voxel.sense.infomsg.utils.UploadUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.TaskFileEntity;
import cyy.example.com.facotry.net.RequestIdFactory;

/**
 * Created by ChenYiYao on 2018/3/29.
 */

public class CheckTaskPresenterSpara extends BasePresenter<CheckTaskContractSpara.View>
        implements CheckTaskContractSpara.Presenter, DataSource.Callback<BaseEntity> {


    public CheckTaskPresenterSpara(CheckTaskContractSpara.View view) {
        super(view);
    }

    @Override
    public void doLoadTaskFile(String taskNum) {
        mView.showLoading("");
        TaskManagerNetHelper.getTaskFile(taskNum, this);
    }


    @Override
    public void doSubmitCheck(String taskNumb, String opinion, boolean flag) {
        String state = flag ? "1" : "0";
        mView.showLoading("");
        TaskManagerNetHelper.checkTask(taskNumb, opinion, state, CheckTaskPresenterSpara.this);
    }

    @Override
    public void doDownDocFile(final String url) {
        final File dir = AppFileUtil.getCacheDir();
        final String fileName = url.substring(url.lastIndexOf("/") + 1);

        //表示请求可以传参，怎么传都行，和okgo使用方法一样
        UploadUtils.getInstent().getPostDownRequest(dir.getAbsolutePath(), fileName, url, new DownloadListener(url) {
            @Override
            public void onStart(Progress progress) {
                if (mView == null)
                    return;
                mView.showLoading("", false);
            }

            @Override
            public void onProgress(Progress progress) {

            }

            @Override
            public void onError(Progress progress) {
                if (mView == null)
                    return;
                mView.dimissLoading();
                ToastUtil.showToastLong(Factory.app(), "下载失败");
                FileUtils.deleteAllFile(new File(dir, fileName));
            }

            @Override
            public void onFinish(File file, Progress progress) {
                if (mView == null)
                    return;
                mView.dimissLoading();
                if (FileUtils.isFileExists(file)) {
                    //文件已存在，直接打开
                    mView.onLookDoc(file);
                }
            }

            @Override
            public void onRemove(Progress progress) {
                mView.dimissLoading();
            }
        });
    }


    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null) {
            return;
        }
        mView.dimissLoading();
        ToastUtil.showToastLong(Factory.app(), R.string.network_error);
    }

    @Override
    public void onDataLoaded(BaseEntity baseEntity, int reqId) {
        if (mView == null) {
            return;
        }
        mView.dimissLoading();
        if (reqId == RequestIdFactory.GET_TASK_FILE) { //获取任务数据
            TaskFileEntity data = (TaskFileEntity) baseEntity;
            if (data != null && data.isSuccess()) {
                List<TaskFileEntity.BodyBean> body = data.getBody();
                handleFileListData(body);
            } else {
                ToastUtil.showToastLong(Factory.app(), "获取任务文件数据失败");
            }
        } else if (reqId == RequestIdFactory.CHECK_TASK) {//提交审核回调
            if (baseEntity.isSuccess()) {
                mView.onSubmitCheck(true);
            } else {
                mView.onSubmitCheck(false);
                ToastUtil.showToastLong(Factory.app(), "审核失败");

            }
        }
    }

    private void handleFileListData(List<TaskFileEntity.BodyBean> body) {
        if (body != null && body.size() > 0) {
            List<String> photoDatas = new ArrayList<>();
            List<String> docuDatas = new ArrayList<>();
            List<String> videoDatas = new ArrayList<>();
            for (TaskFileEntity.BodyBean bean : body) {
                if (Constants.up_load_type_photo.equals(bean.getFileType())) {
                    photoDatas.add(bean.getFilePath());
                } else if (Constants.up_load_type_douc.equals(bean.getFileType())) {
                    docuDatas.add(bean.getFilePath());
                } else if (Constants.up_load_type_vide.equals(bean.getFileType())) {
                    videoDatas.add(bean.getFilePath());
                }
            }
            mView.onRefreshPhoto(photoDatas);
            mView.onRefreshDoc(docuDatas);
            mView.onRefreshVideo(videoDatas);
        }
    }
}

